package org.amse.korovkin.ciphering.view;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.zip.DataFormatException;
import javax.swing.*;
import org.amse.korovkin.ciphering.Main;
import org.amse.korovkin.ciphering.model.*;

public class AdjustementJDialog extends JDialog{
    private static final String ADJUSTEMENT_ICON_PATH = "org/amse/korovkin/ciphering/icons/preview-24.png";
    private static final String REFRESH_ICON_PATH = "org/amse/korovkin/ciphering/icons/refresh-24.png";
    private AttackJDialog parent;
    private JTextArea label = new JTextArea("Select true chosen           symbols");
    private JToggleButton[] alphabet;
    private JButton refresh = new JButton(new ImageIcon(Main.class.getClassLoader()
            .getResource(REFRESH_ICON_PATH)));
    private JTextArea ta = new JTextArea("No any variants for this moment");
    private JScrollPane scroll = new JScrollPane(ta, JScrollPane
            .VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    private final Dimension size = new Dimension(500,550);

    public  AdjustementJDialog(AttackJDialog jFrame) {
        super(jFrame, "Adjustement", true);
        parent = jFrame;
        setSize(size);
        setResizable(true);
        setMinimumSize(new Dimension(size.width / 2, size.height / 2));
        setLocation(parent.getX() + parent.getWidth() / 2 + 30
                , parent.getY() + parent.getHeight() - size.height);

        ta.setEditable(false);
        ta.setRows(100);
        ta.setLineWrap(true);
        ta.setMargin(new Insets(10, 5, 15, 3));
        scroll.setPreferredSize(new Dimension((size.width/5)*4,(size.height/5)*4));

        Container c = getContentPane();
        c.setLayout(new BoxLayout(c, BoxLayout.X_AXIS));
        c.add(createLeftPanel());
        c.add(scroll);
        IModel model = Model.getInstance();
        String filePath = model.getMomentFilePath();
        try {
            if (filePath.charAt(filePath.length() - 1) != '0') {
                ta.read(new FileReader(filePath), filePath);
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        refresh.setToolTipText("Refresh attack with true chosen symbols");
        refresh.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < alphabet.length; i++) {
                    if (alphabet[i].isSelected()) {
                        if ( !alphabet[i].getText().equals("Space") ) {
                            sb.append(alphabet[i].getText().charAt(0));
                        } else {
                            sb.append(" ");
                        }
                    }
                }
                char[] letters = sb.toString().toCharArray();
                dispose();
                IModel model = Model.getInstance();
                if (letters.length != 0) {
                    try {
                        model.setTrueChoices(letters);
                    } catch (DataFormatException ex) {
                        ex.printStackTrace();
                    } catch (InterruptedException ex) {
                        ex.printStackTrace();
                    }
                }
            }
        });
        setVisible(true);
    }

    public static class AdjustementAction extends AbstractAction {
        AttackJDialog parent;
        public AdjustementAction(AttackJDialog jFrame) {
            parent = jFrame;
            putValue(NAME, "Adjustement");
            putValue(SHORT_DESCRIPTION, "View more information and adjust the process");
            ClassLoader cl = Main.class.getClassLoader();
            putValue(LARGE_ICON_KEY, new ImageIcon(cl.getResource(ADJUSTEMENT_ICON_PATH)));
        }
        public void actionPerformed(ActionEvent e) {
            if (!parent.getPauseFlag()) {
                parent.setPause();
            }
            AdjustementJDialog ajd = new AdjustementJDialog(parent);
            parent.pressPause();
            parent.update();
        }
    }
    private Container createLeftPanel() {
        Dimension buttonSize = new Dimension(30,20);
        alphabet = new JToggleButton[PossibilityTable.ALPHABET.size()];
        for (int i = 0; i < alphabet.length - 1; i++) {
            char ch = (char)(i + (int)'a');
            alphabet[i] = new JToggleButton(((Character)ch).toString());
        }
        alphabet[alphabet.length - 1] = new JToggleButton("Space");
        Container alphabetContainer = new Container();
        alphabetContainer.setMinimumSize(new Dimension(size.width/5, size.height - 200));
        alphabetContainer.setMaximumSize(new Dimension(size.width/4, size.height - 150));
        GridLayout gl = new GridLayout(PossibilityTable.ALPHABET.size() / 2,2,2,2);
        alphabetContainer.setLayout(gl);
        for (int i = 0; i < alphabet.length - 1; i++) {
            alphabetContainer.add(alphabet[i]);
        }
        label.setPreferredSize(new Dimension(size.width/4 - 20 , 36));
        label.setLineWrap(true);
        label.setOpaque(false);
        label.setEditable(false);

        Container cont = new Container();
        cont.setLayout(new FlowLayout(FlowLayout.CENTER, 2, 2));
        cont.add(label);
        cont.add(alphabetContainer);
        cont.add(alphabet[alphabet.length - 1]);
        JLabel jl = new JLabel(" ");
        jl.setPreferredSize(new Dimension(100,30));
        cont.add(jl);
        cont.add(refresh);
        cont.setPreferredSize(new Dimension(size.width/4, size.height));
        cont.setMinimumSize(new Dimension(size.width/4, size.height));
        cont.setMaximumSize(new Dimension(size.width/4, size.height * 2));
        return cont;
    }
}
